package com.yc.web;

import com.yc.web.clients.HelloClient;
import com.yc.web.clients.SnowIdClient;
import com.yc.web.controller.model.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequiredArgsConstructor
//@RequestMapping("/test")
public class TestController {
    private final HelloClient helloClient;
    private final SnowIdClient snowIdClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public ResponseResult test() {
        ResponseResult r1 = helloClient.getHello();
        ResponseResult r2 = snowIdClient.getNextId();
        return ResponseResult.ok().setData(r1.getData() + "   " + r2.getData());
    }

    @GetMapping("/restTemplateTest")
    public ResponseResult restTemplateTest() {
        try {
            System.out.println("尝试调用 sayHello-provider...");

            // 手动反序列化
            ResponseResult helloResult = restTemplate.getForObject(
                    "http://sayHello-provider/hello",
                    ResponseResult.class);

            ResponseResult idResult = restTemplate.getForObject(
                    "http://snowflake-provider/id/next",
                    ResponseResult.class);

            String combined = helloResult.getData() + "   " + idResult.getData();
            return ResponseResult.ok().setData(combined);

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("服务调用失败: " + e.getMessage());
        }
    }
}